<template>
    <div>
        <!-- 搜索 -->
        <Search ref="search" :fields="searchItem" :search="getList"></Search>
        <!-- 列表 -->
        <DataTable v-loading="fieldListLoading" :list="fieldList" :fields="fields"
            :permissions="$store.state.permissions" :add="toSave" :exports="toSave" :edit="toSave" :del="del" :batchDel="batchDel">
        </DataTable>
        <!-- 分页 -->
        <el-pagination style="float:right;margin-top: 10px;" background layout="total, prev, pager, next"
            :page-size="objFieldPageSize" :total="objFieldTotal" @current-change="changeObjFieldPage">
        </el-pagination>

        <!-- 新增|修改 -->
        <Form ref="form" :title="title" :visible.sync="formVisible" :fields.sync="formFields" :appendToBody="true"
            :formData="formData" :submitForm="save" 
            :modal="false" :width="'40%'">
        </Form>
    </div>
</template>

<script>
export default {
    props: ['objCode'],
    data(){
        var fieldTypeOptions = [
            {
                value: 'varchar',
                label: '字符串'
            },
            {
                value: 'int',
                label: '整数'
            },
            {
                value: 'bigint',
                label: '长整数'
            },
            {
                value: 'decimal',
                label: '￥ / 小数'
            }, 
            {
                value: 'datetime',
                label: '日期时间'
            }, 
            {
                value: 'tinyint',
                label: '布尔类型'
            },
            {
                value: 'text',
                label: '文本'
            },
            {
                value: 'longtext',
                label: '长文本'
            },
            {
                value: 'base_obj',
                label: '数据对象'
            }
        ];
        let fieldTypeOptionMap = {};
        fieldTypeOptions.forEach(e=>{
            fieldTypeOptionMap[e.value] = e.label;
        });
        return {
            fieldTypeOptionMap: fieldTypeOptionMap,
            searchItem: [
                {
                    label: "属性名称",
                    value: "fieldName",
                    type: "string",
                },
            ],
            fieldListLoading: false,
            fields: [
                {
                    label: "属性名称",
                    value: "fieldName"
                },
                {
                    label: "属性编码",
                    value: "fieldCode"
                },
                {
                    label: "类型",
                    value: "fieldType"
                },
                {
                    label: "长度",
                    value: "fieldLength"
                },
                {
                    label: "小数位",
                    value: "decimalDigits"
                },
                {
                    label: "不为空",
                    value: "notNull",
                    type: "boolean"
                },
                {
                    label: "值唯一",
                    value: "unique",
                    type: "boolean"
                },
                {
                    label: "默认值",
                    value: "defaultValue"
                },
            ],
            fieldList: [],
            objFieldPageNum: 1,
            objFieldPageSize: 5,
            objFieldTotal: 0,

            title: '',
            formVisible: false,
            formFields: [
                {
                    label: "属性名称",
                    value: "fieldName",
                    rules: [{ required: true, message: "属性名称不能为空!" }],
                },
                {
                    label: "类型",
                    value: "fieldType",
                    type: "selector",
                    options: fieldTypeOptions,
                    rules: [{ required: true, message: "类型不能为空!" }],
                },
                {
                    label: "属性编码",
                    value: "fieldCode",
                    rules: [{ required: true, message: "属性名称不能为空!" }],
                },
                {
                    label: "长度",
                    value: "fieldLength",
                    type: "number",
                },
                {
                    label: "小数位",
                    value: "decimalDigits",
                    type: "number",
                },
                {
                    label: "不为空",
                    value: "notNull",
                    type: "radio",
                    options: [
                        {
                            label: "是",
                            value: true,
                        },
                        {
                            label: "否",
                            value: false,
                        }
                    ],
                },
                {
                    label: "值唯一",
                    value: "unique",
                    type: "radio",
                    options: [
                        {
                            label: "是",
                            value: true,
                        },
                        {
                            label: "否",
                            value: false,
                        }
                    ],
                },
                {
                    label: "默认值",
                    value: "defaultValue",
                },
            ],
            formFields1: [
                {
                    label: "属性名称",
                    value: "fieldName",
                    rules: [{ required: true, message: "属性名称不能为空!" }],
                },
                {
                    label: "类型",
                    value: "fieldType",
                    type: "selector",
                    options: fieldTypeOptions,
                    rules: [{ required: true, message: "类型不能为空!" }],
                },
                {
                    label: "属性编码",
                    value: "fieldCode",
                    rules: [{ required: true, message: "属性名称不能为空!" }],
                },
                {
                    label: "长度",
                    value: "fieldLength",
                    type: "number",
                },
                {
                    label: "小数位",
                    value: "decimalDigits",
                    type: "number",
                },
                {
                    label: "不为空",
                    value: "notNull",
                    type: "radio",
                    options: [
                        {
                            label: "是",
                            value: true,
                        },
                        {
                            label: "否",
                            value: false,
                        }
                    ],
                },
                {
                    label: "值唯一",
                    value: "unique",
                    type: "radio",
                    options: [
                        {
                            label: "是",
                            value: true,
                        },
                        {
                            label: "否",
                            value: false,
                        }
                    ],
                },
                {
                    label: "默认值",
                    value: "defaultValue",
                },
            ],
            formFields2:[
                {
                    label: "属性名称",
                    value: "fieldName",
                    rules: [{ required: true, message: "属性名称不能为空!" }],
                },
                {
                    label: "类型",
                    value: "fieldType",
                    type: "selector",
                    options: fieldTypeOptions,
                    rules: [{ required: true, message: "类型不能为空!" }],
                },
                {
                    label: "关联数据对象",
                    value: "correlatedBaseObjCode",
                    type: "selector",
                    options: [],
                    rules: [{ required: true, message: "数据对象不能为空!" }],
                },
                {
                    label: "关联对象属性",
                    value: "correlatedBaseObjFieldCode",
                    type: "selector",
                    options: [],
                    rules: [{ required: true, message: "对象属性不能为空!" }],
                },
                {
                    label: "展示对象属性",
                    value: "defaultValue",
                    type: "selector",
                    options: [],
                    rules: [{ required: true, message: "展示对象属性不能为空!" }],
                },
            ],
            formData: {},
        }
    },
    watch: {
        watchFieldType(fieldType) {
            if (fieldType == "base_obj") {
                this.formFields = this.formFields2
            }else{
                this.formFields = this.formFields1
            }
        },
        watchCorrelatedBaseObjCode(correlatedBaseObjCode) {
            if(correlatedBaseObjCode){
                this.getBaseObjFieldsOptions(correlatedBaseObjCode);
            }
        }
    },
    computed:{
        watchFieldType() {
            return this.formData.fieldType
        },
        watchCorrelatedBaseObjCode(){
            return this.formData.correlatedBaseObjCode
        }
    },
    mounted() {
        this.getList();
        this.getBaseObjOptions();
    },
    methods: {
        save(){
            this.formData["objCode"] = this.objCode;
            if(this.formData.fieldType == "base_obj"){
                this.formData.fieldCode = this.formData.defaultValue
            }
            this.$api.baseObjField.save(this.formData).then(res => {
                if (res.code == 200) {
                    this.$message.success(res.msg);
                    this.formVisible = false;
                    this.$refs.search.search(this.$refs.search.param);
                } else {
                    this.$message.error(res.msg);
                    this.$refs.form.saveLoading = false;
                }
            })
        },
        toSave(uuid){
            this.formData = {
                fieldLength: 0,
                decimalDigits: 0,
                notNull: false,
                orderBy: false,
                unique: false,
            }
            if(uuid){
                this.title = "修改对象属性"
                this.$api.baseObjField.one(this.objCode, uuid).then(res => {
                    if(res.code==200){
                        this.formData = res.data;
                    }else{
                        this.$message.error(res.msg);
                    }
                })
            }else{
                this.title = "新增对象属性"
            }
            this.formVisible = true;
        },
        del(uuid){
            this.$api.baseObjField.del({ uuid: uuid }).then(res => {
                if (res.code == 200) {
                    this.$message.success(res.msg);
                    this.$refs.search.search(this.$refs.search.param);
                } else {
                    this.$message.error(res.msg);
                }
            })
        },
        // 批量删除
        batchDel(uuids){
            let vo = {
                objectCode: "base_obj_field",
                conditions: [
                    {
                        fieldCode: "uuid",
                        operator: "in",
                        value: uuids,
                    }
                ]
            }
            this.$api.baseData.del(vo).then(res => {
                if (res.code == 200) {
                    this.$message.success(res.msg);
                    this.$refs.search.search(this.$refs.search.param);
                } else {
                    this.$message.error(res.msg);
                }
            })
        },
        getList(param) {
            this.fieldListLoading = true;
            param = param ? param : {};
            param.pageNum = this.objFieldPageNum;
            param.pageSize = this.objFieldPageSize;
            this.fieldList = [];
            this.$api.baseObjField.list(this.objCode, param).then(res => {
                if(res.code==200){
                    this.fieldList = res.data.list;
                    this.fieldList.forEach(e=>{
                        e.fieldType = this.fieldTypeOptionMap[e.fieldType];
                    })
                    this.objFieldPageNum = res.data.pageNum;
                    this.objFieldTotal = res.data.total;
                }else{
                    this.$message.error(res.msg);
                }
                this.fieldListLoading = false;
            })
        },
        getBaseObjOptions() {
            let param = {
                pageNum: 1,
                pageSize: 99999,
            }
            this.$api.baseObj.list(param).then(res => {
                if (res.code == 200) {
                    let baseObjOPtions = [];
                    res.data.list.forEach(e =>{
                        baseObjOPtions.push({
                            value: e.objCode,
                            label: e.objName
                        })
                    })
                    this.formFields2[2].options = baseObjOPtions;
                }
            })
        },
        getBaseObjFieldsOptions(objCode) {
            let param = {
                pageNum: 1,
                pageSize: 99999,
            }
            this.$api.baseObjField.list(objCode, param).then(res => {
                if(res.code==200){
                    let baseObjFieldsOPtions = [];
                    res.data.list.forEach(e =>{
                        baseObjFieldsOPtions.push({
                            value: e.fieldCode,
                            label: e.fieldName
                        })
                    })
                    this.formFields2[3].options = baseObjFieldsOPtions;
                    this.formFields2[4].options = baseObjFieldsOPtions;
                }else{
                    this.$message.error(res.msg);
                }
            })
        },
        changeObjFieldPage(pageNum) {
            this.objFieldPageNum = pageNum;
            this.$refs.search.search(this.$refs.search.param);
        },
    }
}
</script>